﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Transitions;

namespace UIT_CV_Management
{
    public partial class RegisterForm : Form
    {
        public bool GotError; //for displaying tips

        public bool ValidUsername;
        public bool ValidPassword;
        public bool ValidEmail;
        public bool ValidConfirm;

        public String CurrentTip = "REGISTER_FORM_TIP_INIT";
        bool invalid;

        public RegisterForm()
        {
            InitializeComponent();
            InitLanguage();
        }

        public void InitLanguage()
        {
            this.Text = Language._("REGISTER_FORM_TITLE");
            lblTitle.Text = Language._("REGISTER_FORM_TITLE_LBL");
            lblEmail.Text = Language._("REGISTER_FORM_EMAIL_LBL");
            lblUsername.Text = Language._("REGISTER_FORM_USERNAME_LBL");
            lblPassword.Text = Language._("REGISTER_FORM_PASSWORD_LBL");
            lblConfirm.Text = Language._("REGISTER_FORM_CONFIRM_LBL");
            btnRegister.Text = Language._("REGISTER_FORM_REGISTER_BTN");
            btnCancel.Text = Language._("REGISTER_FORM_CANCEL_BTN");
            utpTip.Value = Language._(CurrentTip);
        }
        /// <summary>
        /// Quay lại form Login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Đăng Ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (!ValidUsername || !ValidEmail || !ValidPassword || !ValidConfirm)
                return;
            User usr = new User(tbxUsername.Text, tbxPassword.Text, tbxEmail.Text, true);
            if (usr.Save())
                wMessageBox.Show(this, Language._("REGISTER_FORM_REGISTRATION_COMPLETED"), 
                    Language._("REGISTER_FORM_REGISTRATION_COMPLETE_TITLE"), 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        
        private void tbxEmail_Validating(object sender, CancelEventArgs e)
        {
            if (tbxEmail.Text == "")
            {
                GotError = true;
                ValidEmail = false;
                utpTip.Value = Language._("REGISTER_FORM_TIP_EMAIL_NULL");
                tbxEmail.BackColor = Color.Tomato;
                ptbEmail.Visible = false;
                return;
            }
            if (IsValidEmail(tbxEmail.Text)) {
                GotError = false;
                ValidEmail = true;
                ptbEmail.Visible = true;
                tbxEmail.BackColor = Color.White;
            }
            else {
                GotError = true;
                ValidEmail = false;
                utpTip.Value = Language._("REGISTER_FORM_TIP_EMAIL_WRONG_FORMAT");
                tbxEmail.BackColor = Color.Tomato;
                ptbEmail.Visible = false;
            }
        }

        public bool IsValidEmail(string strIn)
        {
            invalid = false;
            if (String.IsNullOrEmpty(strIn))
                return false;

            // Use IdnMapping class to convert Unicode domain names.
            strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper);
            if (invalid)
                return false;

            // Return true if strIn is in valid e-mail format. 
            return Regex.IsMatch(strIn,
                   @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
                   RegexOptions.IgnoreCase);
        }

        private string DomainMapper(Match match)
        {
            // IdnMapping class with default property values.
            IdnMapping idn = new IdnMapping();

            string domainName = match.Groups[2].Value;
            try
            {
                domainName = idn.GetAscii(domainName);
            }
            catch (ArgumentException)
            {
                invalid = true;
            }
            return match.Groups[1].Value + domainName;
        }

        private void tbxEmail_Enter(object sender, EventArgs e)
        {
            if (!GotError)
                utpTip.Value = Language._("REGISTER_FORM_TIP_EMAIL");
        }

        private void tbxUsername_Enter(object sender, EventArgs e)
        {
            if (!GotError)
                utpTip.Value = Language._("REGISTER_FORM_TIP_USERNAME");
        }

        private void tbxPassword_Enter(object sender, EventArgs e)
        {
            if (!GotError)
                utpTip.Value = Language._("REGISTER_FORM_TIP_PASSWORD");
        }

        private void tbxConfirm_Enter(object sender, EventArgs e)
        {
            if (!GotError)
                utpTip.Value = Language._("REGISTER_FORM_TIP_CONFIRM");
        }

        private void tbxUsername_Validating(object sender, CancelEventArgs e)
        {
            if (tbxUsername.Text == "")
            {
                GotError = true;
                ValidUsername = false;
                utpTip.Value = Language._("REGISTER_FORM_TIP_USERNAME_NULL");
                tbxUsername.BackColor = Color.Tomato;
                ptbUsername.Visible = false;
            }
            else
            {
                User usr = new User(tbxUsername.Text);
                if (usr.ID == null)
                {
                    GotError = false;
                    ValidUsername = true;
                    tbxUsername.BackColor = Color.White;
                    ptbUsername.Visible = true;
                }
                else
                {
                    GotError = true;
                    ValidUsername = false;
                    utpTip.Value = Language._("REGISTER_FORM_TIP_USERNAME_REGISTERED");
                    tbxUsername.BackColor = Color.Tomato;
                    ptbUsername.Visible = false;
                }
            }
        }

        private void tbxPassword_Validating(object sender, CancelEventArgs e)
        {
            if (tbxPassword.Text == "")
            {
                GotError = true;
                ValidPassword = false;
                utpTip.Value = Language._("REGISTER_FORM_TIP_PASSWORD_NULL");
                tbxPassword.BackColor = Color.Tomato;
                ptbPassword.Visible = false;
            }
            else
            {
                GotError = false;
                ValidPassword = true;
                tbxPassword.BackColor = Color.White;
                ptbPassword.Visible = true;
            }
        }

        private void tbxConfirm_Validating(object sender, CancelEventArgs e)
        {
            if (tbxConfirm.Text != tbxPassword.Text || tbxConfirm.Text == "")
            {
                GotError = true;
                ValidConfirm = false;
                utpTip.Value = Language._("REGISTER_FORM_TIP_PASSWORD_NOT_MATCH");
                tbxConfirm.BackColor = Color.Tomato;
                ptbConfirm.Visible = false;
            }
            else
            {
                GotError = false;
                ValidConfirm = true;
                tbxConfirm.BackColor = Color.White;
                utpTip.Value = Language._("REGISTER_FORM_TIP_DOUBLE_CHECK");
                ptbConfirm.Visible = true;
            }
        }
    }
}
